package com.papers.juan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.papers.common.constant.UserConstants;
import com.papers.common.core.domain.PageQuery;
import com.papers.common.core.page.TableDataInfo;
import com.papers.common.enums.PaperRecordType;
import com.papers.common.helper.LoginHelper;
import com.papers.common.utils.StringUtils;
import com.papers.juan.domain.TJuanPaperRecord;
import com.papers.juan.domain.bo.TJuanPaperRecordBo;
import com.papers.juan.domain.vo.TJuanPaperRecordVo;
import com.papers.juan.mapper.TJuanPaperRecordMapper;
import com.papers.juan.service.ITJuanPaperRecordService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 试卷维护记录Service业务层处理
 *
 * @author papers
 * @date 2022-10-08
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class TJuanPaperRecordServiceImpl implements ITJuanPaperRecordService {

    private final TJuanPaperRecordMapper tJuanPaperRecordMapper;

    /**
     * 查询试卷维护记录
     */
    @Override
    public TJuanPaperRecordVo queryById(String recordId) {
        return tJuanPaperRecordMapper.selectVoById(recordId);
    }

    /**
     * 查询试卷维护记录列表
     */
    @Override
    public TableDataInfo<TJuanPaperRecordVo> queryPageList(TJuanPaperRecordBo bo, PageQuery pageQuery) {
//        LambdaQueryWrapper<TJuanPaperRecord> lqw = buildQueryWrapper(bo);
        if (!LoginHelper.getUserId().equals(UserConstants.ADMIN_ID)) {
            bo.setCreatedBy(String.valueOf(LoginHelper.getUserId()));
        }
        Page<TJuanPaperRecordVo> result = tJuanPaperRecordMapper.queryPageList(bo, pageQuery.build());
        return TableDataInfo.build(result);
    }

    /**
     * 导出组卷维护记录列表
     *
     * @return 组卷维护记录列表
     */
    @Override
    public List<TJuanPaperRecordVo> exportGeneratePaperRecords() {
        List<TJuanPaperRecordVo> result = tJuanPaperRecordMapper.queryPageList(new TJuanPaperRecordBo(),
            new PageQuery().build()).getRecords();
        return result.stream().filter(item -> PaperRecordType.MANUAL_INSERT.getKey().equals(item.getRecordType())
            || PaperRecordType.AUTO_GENERATE.getKey().equals(item.getRecordType())).collect(Collectors.toList());
    }

    /**
     * 根据试卷id获取试卷维护记录列表
     *
     * @param paperId   试卷id
     * @param pageQuery 分页条件
     * @return 试卷维护记录列表
     */
    @Override
    public TableDataInfo<TJuanPaperRecordVo> queryPaperRecordListByPaperId(String paperId, PageQuery pageQuery) {
        Page<TJuanPaperRecordVo> recordList = tJuanPaperRecordMapper.queryPaperRecordListByPaperId(paperId, pageQuery.build());
        log.info("{}根据试卷id获取试卷维护记录列表", LoginHelper.getUsername());
        return TableDataInfo.build(recordList);
    }

    /**
     * 查询试卷维护记录列表
     */
    @Override
    public List<TJuanPaperRecordVo> queryList(TJuanPaperRecordBo bo) {
        LambdaQueryWrapper<TJuanPaperRecord> lqw = buildQueryWrapper(bo);
        return tJuanPaperRecordMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TJuanPaperRecord> buildQueryWrapper(TJuanPaperRecordBo bo) {
//        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TJuanPaperRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getPaperId()), TJuanPaperRecord::getPaperId, bo.getPaperId());
        lqw.eq(StringUtils.isNotBlank(bo.getRecordType()), TJuanPaperRecord::getRecordType, bo.getRecordType());
        lqw.eq(StringUtils.isNotBlank(bo.getRecordContent()), TJuanPaperRecord::getRecordContent, bo.getRecordContent());
        lqw.eq(StringUtils.isNotBlank(bo.getCreatedBy()), TJuanPaperRecord::getCreatedBy, bo.getCreatedBy());
        lqw.eq(bo.getCreatedTime() != null, TJuanPaperRecord::getCreatedTime, bo.getCreatedTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdatedBy()), TJuanPaperRecord::getUpdatedBy, bo.getUpdatedBy());
        lqw.eq(bo.getUpdatedTime() != null, TJuanPaperRecord::getUpdatedTime, bo.getUpdatedTime());
        return lqw;
    }

    /**
     * 新增试卷维护记录
     */
    @Override
    public Boolean insertByBo(TJuanPaperRecordBo bo) {
        TJuanPaperRecord add = BeanUtil.toBean(bo, TJuanPaperRecord.class);
        validEntityBeforeSave(add);
        boolean flag = tJuanPaperRecordMapper.insert(add) > 0;
        if (flag) {
            bo.setRecordId(add.getRecordId());
        }
        return flag;
    }

    /**
     * 修改试卷维护记录
     */
    @Override
    public Boolean updateByBo(TJuanPaperRecordBo bo) {
        TJuanPaperRecord update = BeanUtil.toBean(bo, TJuanPaperRecord.class);
        validEntityBeforeSave(update);
        return tJuanPaperRecordMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TJuanPaperRecord entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除试卷维护记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return tJuanPaperRecordMapper.deleteBatchIds(ids) > 0;
    }
}
